<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 头部 -->
        <treeNode :tree-node="company" :is-root="true" @AddDept="AddDept" />
        <!-- 分割线 -->
        <hr>
        <!-- 结构 -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
          :expand-on-click-node="false"
        >
          <template #default="scoped">
            <treeNode
              :tree-node="scoped.data"
              @delDepartment="getDepartments"
              @AddDept="AddDept"
              @editDepartments="editDepartments"
            />
          </template>
        </el-tree>
      </el-card>
      <AddDept ref="addDept" :show-diolog.sync="showDiolog" :tree-node="treeNode" @addelDepartment="getDepartments" />
    </div>
  </div>
</template>

<script>
import treeNode from './components/treeNode'
import AddDept from './components/add-dept'
import { getDepartments } from '@/api/departments'
import { listToTreeData } from '@/utils/index'
export default {
  components: {
    treeNode,
    AddDept
  },
  data() {
    return {
      showDiolog: false,
      departs: [],
      treeNode: {},
      defaultProps: {
        label: 'name', // 表示 从这个属性显示内容
        children: 'children' // 子目录属性显示内容
      },
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' }
    }
  },
  created() {
    // 渲染页面
    this.getDepartments()
  },
  methods: {
    //  获取组织架构数据
    async getDepartments() {
      const res = await getDepartments()
      this.departs = listToTreeData(res.depts, '')
      // console.log(this.departs)
    },
    AddDept(treeNode) {
      this.showDiolog = true
      this.treeNode = treeNode
    },
    editDepartments(treeNode) {
      this.showDiolog = true
      this.treeNode = treeNode
      this.$refs.addDept.getDepartDetail(treeNode.id)
    }
  }

}
</script>
<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
